package com.mangofactory.aop.configuration.tag
{
	import com.mangofactory.aop.aspects.Aspect;
	import com.mangofactory.aop.configuration.AspectContainerBuilder;
	import com.mangofactory.aop.configuration.PatternMatchingProxyPolicy;
	import com.mangofactory.aop.configuration.PatternMatchingProxyPolicyBuilder;
	
	public class MethodPolicyTagProcessor implements PolicyTagProcessor
	{
		public function isForTag(tag:PolicyProviderTag):Boolean
		{
			return tag is MethodPolicyTag;
		}
		
		public function processTag(tag:PolicyProviderTag, builder:AspectContainerBuilder):void
		{
			var methodTag:MethodPolicyTag = MethodPolicyTag(tag);
			if (methodTag.aspect && methodTag.aspects)
			{
				throw new Error("You can specify only aspcet or aspects, but not both");
			}
			if (!methodTag.aspect && !methodTag.aspects)
			{
				throw new Error("Etiher aspect or aspects must be provided");
			}
			var methodPattern:String = methodTag.methodPattern || PatternMatchingProxyPolicy.ANY_METHOD;
			var aspects:Vector.<Aspect> = Vector.<Aspect>(methodTag.aspects) || Vector.<Aspect>([methodTag.aspect]);
			
			var policy:PatternMatchingProxyPolicy = new PatternMatchingProxyPolicy(methodTag.classPattern,methodPattern,aspects);
			builder.addProxyPolicy(policy);
		}
	}
}